<?php
namespace app\common\logic;



use app\common\logic\cast\news\HotTime;
use app\common\utils\Utils;
use think\Db;
use think\exception\ValidateException;

/**
 * Created by PhpStorm.
 * User: yangchunfu
 * Date: 2019/12/24
 * Time: 4:36 PM
 * 栏目api
 */

class NewsApi
{
    public $classConfig = [];
    protected $row = 15;
    /**
     * 对象
     * @var \app\common\logic\cast\Base
     */
    private $class = null;
    public function __construct($tag = '', $options = [])
    {
        $this->classConfig = config('news_config');
        $class = $this->classConfig[$tag];
        if (!$class) {
            throw new ValidateException('标示传递错误');
        }

        $class = '\\app\\common\\logic\\cast\\news\\' . ucfirst($class);

        $this->class = new $class($options, $tag);
        $this->row = input('row/d', 15);
    }

    public function apiData() {
        return $this->class->get();
    }



    public function getList() {
        return $this->class->getList($this->row);
    }
    public function add($category_id, $item_id) {
        $ids = Utils::explodeStr($category_id);
        if (!$ids) {
            throw new ValidateException('请传递栏目所属分类');
        }
        if (!$item_id) {
            throw new ValidateException('请传递栏目id');
        }
        Db::name('self_cate_to_item')->where([
            'item_id' => $item_id
        ])->delete();

        $all = [];
        foreach ($ids as $k => $v) {
            $data['category_id'] = $v;
            $data['item_id'] = $item_id;
            array_push($all, $data);
        }

        $res = Db::name('self_cate_to_item')->insertAll($all);
        if (!$res) {
            throw new ValidateException('保存失败');
        }

        return true;
    }


}